from flask import Flask,g

app = Flask(__name__)

@app.url_defaults
def add_language_code(endpoint, values):
    print("endpoint:{}".format(endpoint))
    print("values:{}".format(values))
    if 'lang_code' in values or not g.lang_code:
        return
    if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
        values['lang_code'] = g.lang_code

@app.url_value_preprocessor
def pull_lang_code(endpoint, values):
    print("-endpoint:{}".format(endpoint))
    print("-values:{}".format(values))
    g.lang_code = values.pop('lang_code', None)

@app.route('/<lang_code>/')
def index():
    print(g.lang_code)
    return 'index' + g.lang_code

@app.route('/<lang_code>/about')
def about():
    print(g.lang_code)
    return 'about' + g.lang_code

if __name__ == "__main__":
    app.run(debug=True)